﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Implementation
{
    using System;
    using System.Collections.Generic;
    using ConnectBasic.Shell.Extensibility;

    internal class DefaultShellStateCollection : IShellStateCollection
    {
        IShellState mActiveState;
        private List<IShellState> mStates = new List<IShellState>();
        private Dictionary<string, IShellState> mStatesByName = new Dictionary<string, IShellState>(StringComparer.OrdinalIgnoreCase);

        #region .ctor

        internal DefaultShellStateCollection()
        {
        }

        #endregion

        #region Internal Methods

        internal IShellState Add(string invariantName)
        {
            IShellState state = new DefaultShellState(invariantName);
            mStatesByName.Add(invariantName, state);
            mStates.Add(state);
            return state;
        }

        internal void ActivateState(string invariantName)
        {
            mActiveState = this[invariantName];

            // Make sure the UI gets a chance to update
            EventHandler evt = ActiveStateChanged;

            if (evt != null)
            {
                evt(this, EventArgs.Empty);
            }
        }

        #endregion

        #region IShellStateCollection Members

        public event EventHandler ActiveStateChanged;

        public IShellState ActiveState
        {
            get { return mActiveState; }
        }

        public IShellState this[int index]
        {
            get { return mStates[index]; }
        }

        public IShellState this[string invariantName]
        {
            get { return mStatesByName.ItemOrDefault(invariantName); }
        }

        public int Count
        {
            get { return mStates.Count; }
        }

        #endregion

        #region IEnumerable<IShellState> Members

        IEnumerator<IShellState> IEnumerable<IShellState>.GetEnumerator()
        {
            return mStates.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return mStates.GetEnumerator();
        }

        #endregion

    }
}
